package db.jpa;

import db.jdbc.JdbcStudent;
import db.model.Student;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Persistence;

import java.sql.SQLException;

public class Student4JPA {
    EntityManagerFactory factory;

    public Student4JPA() {
        factory = Persistence.createEntityManagerFactory("demo");
    }

    // 插入student对象到表中
    public void insert(Student student) throws SQLException {
        EntityManager manager = factory.createEntityManager();
        EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        manager.persist(student);
        transaction.commit();
        manager.close();
    }

    // 删除指定id的记录
    public void delete(int id) throws SQLException {

    }

    // 修改表数据，类似insert
    public void update(Student student) throws SQLException {

    }

    // 按 id 查询学生信息，把查到的数据保存到Student对象中，
    // 参考：StudentDAO 的 createStudent 方法
    public JdbcStudent findById(int id) throws SQLException {
        return new JdbcStudent();
    }

    public void insertPhoto() {
        // 插入数据时同时插入图片，这个不好编写测试，各位同学完成后，可以使用
        // StudentView 验证
    }
}
